package com.ruoyi.info.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.info.mapper.CmsCaptchaMapper;
import com.ruoyi.info.domain.CmsCaptcha;
import com.ruoyi.info.service.ICmsCaptchaService;
import com.ruoyi.common.core.text.Convert;

/**
 * 验证码-Service业务层处理
 * 
 * @author xwj
 * @date 2023-07-17
 */
@Service
public class CmsCaptchaServiceImpl implements ICmsCaptchaService 
{
    @Autowired
    private CmsCaptchaMapper cmsCaptchaMapper;

    /**
     * 查询验证码-
     * 
     * @param id 验证码-主键
     * @return 验证码-
     */
    @Override
    public CmsCaptcha selectCmsCaptchaById(Long id)
    {
        return cmsCaptchaMapper.selectCmsCaptchaById(id);
    }

    /**
     * 查询验证码-列表
     * 
     * @param cmsCaptcha 验证码-
     * @return 验证码-
     */
    @Override
    public List<CmsCaptcha> selectCmsCaptchaList(CmsCaptcha cmsCaptcha)
    {
        return cmsCaptchaMapper.selectCmsCaptchaList(cmsCaptcha);
    }

    /**
     * 新增验证码-
     * 
     * @param cmsCaptcha 验证码-
     * @return 结果
     */
    @Override
    public int insertCmsCaptcha(CmsCaptcha cmsCaptcha)
    {
        return cmsCaptchaMapper.insertCmsCaptcha(cmsCaptcha);
    }

    /**
     * 修改验证码-
     * 
     * @param cmsCaptcha 验证码-
     * @return 结果
     */
    @Override
    public int updateCmsCaptcha(CmsCaptcha cmsCaptcha)
    {
        return cmsCaptchaMapper.updateCmsCaptcha(cmsCaptcha);
    }

    /**
     * 批量删除验证码-
     * 
     * @param ids 需要删除的验证码-主键
     * @return 结果
     */
    @Override
    public int deleteCmsCaptchaByIds(String ids)
    {
        return cmsCaptchaMapper.deleteCmsCaptchaByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除验证码-信息
     * 
     * @param id 验证码-主键
     * @return 结果
     */
    @Override
    public int deleteCmsCaptchaById(Long id)
    {
        return cmsCaptchaMapper.deleteCmsCaptchaById(id);
    }

    @Override
    public String createCaptcha(String username) {
        String code = getRandomString(4);

        Date now = new Date();

        CmsCaptcha captcha = new CmsCaptcha();
        captcha.setUkey(username);
        captcha.setCode(code);
        captcha.setExpireTime(DateUtils.addMinutes(now, 8)); // 8分钟有效期

        insertCmsCaptcha(captcha);

        return code;
    }

    @Override
    public String validateCaptcha(String username, String code) {

        CmsCaptcha captcha = cmsCaptchaMapper.getLastCaptchaByKey(username);

        if(captcha == null) {
            return "没有找到验证码!";
        }

        if(!code.equals(captcha.getCode())) {
            return "验证码验证失败!";
        }

        Date now = new Date();
        long nowTime = now.getTime();
        long expire = captcha.getExpireTime().getTime();
        if(nowTime > expire) {
            return "验证码已过期";
        }

        return null;
    }

    private String getRandomString(int length)
    {
        String str="0123456789";
        Random random=new Random();
        StringBuffer sf=new StringBuffer();
        for(int i=0;i<length;i++)
        {
            int number=random.nextInt(9);//0~61
            sf.append(str.charAt(number));


        }
        return sf.toString();
    }
}
